/*
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *  under the License.
 */
package au.com.domlogic.helloPVCWorld.builder;

import au.com.domlogic.pvc.core.component.ComponentMold;
import au.com.domlogic.pvc.core.element.ElementMold;
import au.com.domlogic.pvc.xhtml.component.builder.ComponentTreeMoldBuilder;
import au.com.domlogic.pvc.xhtml.element.SpanElementMold;

/**
 * This class Overrides the behaviour of the standard ComponentTreeModelBuilder
 * by using spans to show elements with text as span is inline rather than block.
 * This builder also actually includes the text content rather than just
 * displaying the component structure.
 *
 * @author Domenic Figliomeni
 */
public class ExampleTreeViewBuilder extends ComponentTreeMoldBuilder {

    private boolean showTextContent;

    @Override
    protected ComponentMold produceElementMoldPreChildrenView(ElementMold elementMold) {
        ComponentMold preChildrenView = super.produceElementMoldPreChildrenView(elementMold);
        if (elementMold.getContent() != null && !elementMold.getContent().equals("") && showTextContent) {
            String elementLabel = elementMold.getTagName();
            SpanElementMold span = new SpanElementMold();
            span.setContent("<" + elementLabel + ">" + elementMold.getContent());
            preChildrenView = span;
        }
        return preChildrenView;
    }

    @Override
    protected ComponentMold produceElementMoldPostChildrenView(ElementMold elementMold) {
        ComponentMold postChildrenView = super.produceElementMoldPostChildrenView(elementMold);
        if (elementMold.getContent() != null && !elementMold.getContent().equals("") && showTextContent) {
            String elementLabel = elementMold.getTagName();
            SpanElementMold span = new SpanElementMold();
            span.setContent("</" + elementLabel + ">");
            postChildrenView = span;
        }
        return postChildrenView;
    }

    public boolean isShowTextContent() {
        return showTextContent;
    }

    public void setShowTextContent(boolean showTextContent) {
        this.showTextContent = showTextContent;
    }
}
